package com.jeewms.www.tms.ui.acitivity;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.annotation.NonNull;
import android.support.constraint.solver.widgets.ConstraintAnchor;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.blankj.utilcode.util.ToastUtils;
import com.jeewms.www.tms.R;
import com.jeewms.www.tms.base.BaseBackActivity;
import com.jeewms.www.tms.bean.bean.MessageEvent;
import com.jeewms.www.tms.bean.listvm.DemoListVm;
import com.jeewms.www.tms.bean.vm.DemoVm;
import com.jeewms.www.tms.bean.vm.Zhmsm;
import com.jeewms.www.tms.bean.vm.wmapiUtil.ResultDO;
import com.jeewms.www.tms.constance.ClassHolder;
import com.jeewms.www.tms.constance.Constance;
import com.jeewms.www.tms.ui.adapter.DemoAdapter;
import com.jeewms.www.tms.util.ClickFastUtil;
import com.jeewms.www.tms.util.GsonUtils;
//import com.jeewms.www.tms.util.LoadingUtil;
import com.jeewms.www.tms.util.Logutil;
import com.jeewms.www.tms.util.OkHttpListener;
import com.jeewms.www.tms.util.OkHttpUtil;
import com.jeewms.www.tms.util.PermissionUtils;
import com.jeewms.www.tms.util.SharedPreferencesUtil;
import com.jeewms.www.tms.util.StringUtil;
import com.jeewms.www.tms.util.ToastUtil;
import com.jeewms.www.tms.util.UpdataUiUtil;
import com.jeewms.www.tms.util.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.jeewms.www.tms.util.z_toast;
import com.jeewms.www.tms.volley.HTTPUtils;
import com.jeewms.www.tms.volley.VolleyListener;
import com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.OnDismissCallback;
import com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.SwipeDismissAdapter;
import com.zhy.android.percent.support.PercentLinearLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 13799 on 2018/8/5.
 */

public class ShangjiaAcitivity extends BaseBackActivity implements OnDismissCallback {


    @BindView(R.id.ll_scan)
    LinearLayout llScan;

    @BindView(R.id.tv_jiaojiedan)
    TextView tvJiaojiedan;
    @BindView(R.id.et_jiaojiedan)
    EditText etJiaojiedan;
    @BindView(R.id.et_fangweima)
    EditText etFangweima;
//  扩充字段
    @BindView(R.id.tv_xianghao)
    TextView tvxianghao;
    @BindView(R.id.et_xianghao)
    EditText etxianghao;

    @BindView(R.id.ll_xianghao)
    PercentLinearLayout llxianghao;
    @BindView(R.id.div_xianghao)
    ImageView divxianghao;
//  扩充字段






    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.lv_mylistview)
    ListView lvMylistview;
    DemoAdapter adapter;
    List<DemoVm> list = new ArrayList<>();
    @BindView(R.id.ll_jiaojiedan)
    PercentLinearLayout llJiaojiedan;
    @BindView(R.id.tv_chuwei)
    TextView tvChuwei;
    @BindView(R.id.et_chuwei)
    EditText etChuwei;
    @BindView(R.id.ll_chuwei)
    PercentLinearLayout llChuwei;
    @BindView(R.id.div_chuwei)
    ImageView divChuwei;
    @BindView(R.id.ll_search)
    PercentLinearLayout llSearch;
    @BindView(R.id.div_search)
    ImageView divSearch;
    @BindView(R.id.tv_wuliaohao)
    TextView tvWuliaohao;
    @BindView(R.id.tv_jiaojieshu)
    TextView tvJiaojieshu;
    @BindView(R.id.tv_daishangjia)
    TextView tvDaishangjia;
    @BindView(R.id.tv_heji)
    TextView tvHeji;
    @BindView(R.id.ll_wuliaohao)
    PercentLinearLayout llWuliaohao;
    @BindView(R.id.tv_fangweima)
    TextView tvFangweima;
    @BindView(R.id.div_jiaojiedan)
    ImageView divJiaojiedan;
    @BindView(R.id.tv_werks)
    TextView tvWerks;
    @BindView(R.id.tv_lgort)
    TextView tvLgort;
    @BindView(R.id.tv_search2)
    TextView tvSearch2;
    @BindView(R.id.head_holder)
    LinearLayout headHolder;
    @BindView(R.id.head)
    LinearLayout head;
    @BindView(R.id.et_werks2)
    EditText etWerks2;
    @BindView(R.id.et_lgort2)
    EditText etLgort2;
    @BindView(R.id.et_chuwei2)
    EditText etChuwei2;
    @BindView(R.id.et_fangweima2)
    EditText etFangweima2;
    private String className;
    private String type;


    private String returns = "{\"errorCode\":\"errorCode\",\"ok\":true,\"errorMsg\":\"errorMsg\",\"obj\":[{\"wmX1\":\"id1\",\"wmX2\":\"wmX2\",\"wmX3\":\"wmX3\",\"wmX4\":\"wmX4\",\"wmX5\":\"wmX5\",\"wmX6\":\"wmX6\",\"wmX7\":\"wmX7\",\"wmX8\":\"wmX8\",\"wmi1List\":[{\"wmX1\":\"wmX1\",\"wmX2\":\"wmX2\",\"wmX3\":\"wmX3\",\"wmX4\":\"wmX4\",\"wmX5\":\"wmX5\",\"wmX6\":\"wmX6\",\"wmX7\":\"wmX7\",\"wmX8\":\"wmX8\"},{\"wmX1\":\"wmX1\",\"wmX2\":\"wmX2\",\"wmX3\":\"wmX3\",\"wmX4\":\"wmX4\",\"wmX5\":\"wmX5\",\"wmX6\":\"wmX6\",\"wmX7\":\"wmX7\",\"wmX8\":\"wmX8\"},{\"wmX1\":\"wmX1\",\"wmX2\":\"wmX2\",\"wmX3\":\"wmX3\",\"wmX4\":\"wmX4\",\"wmX5\":\"wmX5\",\"wmX6\":\"wmX6\",\"wmX7\":\"wmX7\",\"wmX8\":\"wmX8\"}]},{\"wmX1\":\"id2\",\"wmX2\":\"wmX2\",\"wmX3\":\"wmX3\",\"wmX4\":\"wmX4\",\"wmX5\":\"wmX5\",\"wmX6\":\"wmX6\",\"wmX7\":\"wmX7\",\"wmX8\":\"wmX8\",\"wmi1List\":[{\"wmX1\":\"wmX1\",\"wmX2\":\"wmX2\",\"wmX3\":\"wmX3\",\"wmX4\":\"wmX4\",\"wmX5\":\"wmX5\",\"wmX6\":\"wmX6\",\"wmX7\":\"wmX7\",\"wmX8\":\"wmX8\"},{\"wmX1\":\"wmX1\",\"wmX2\":\"wmX2\",\"wmX3\":\"wmX3\",\"wmX4\":\"wmX4\",\"wmX5\":\"wmX5\",\"wmX6\":\"wmX6\",\"wmX7\":\"wmX7\",\"wmX8\":\"wmX8\"},{\"wmX1\":\"wmX1\",\"wmX2\":\"wmX2\",\"wmX3\":\"wmX3\",\"wmX4\":\"wmX4\",\"wmX5\":\"wmX5\",\"wmX6\":\"wmX6\",\"wmX7\":\"wmX7\",\"wmX8\":\"wmX8\"}]},{\"wmX1\":\"id3\",\"wmX2\":\"wmX2\",\"wmX3\":\"wmX3\",\"wmX4\":\"wmX4\",\"wmX5\":\"wmX5\",\"wmX6\":\"wmX6\",\"wmX7\":\"wmX7\",\"wmX8\":\"wmX8\",\"wmi1List\":[{\"wmX1\":\"wmX1\",\"wmX2\":\"wmX2\",\"wmX3\":\"wmX3\",\"wmX4\":\"wmX4\",\"wmX5\":\"wmX5\",\"wmX6\":\"wmX6\",\"wmX7\":\"wmX7\",\"wmX8\":\"wmX8\"},{\"wmX1\":\"wmX1\",\"wmX2\":\"wmX2\",\"wmX3\":\"wmX3\",\"wmX4\":\"wmX4\",\"wmX5\":\"wmX5\",\"wmX6\":\"wmX6\",\"wmX7\":\"wmX7\",\"wmX8\":\"wmX8\"},{\"wmX1\":\"wmX1\",\"wmX2\":\"wmX2\",\"wmX3\":\"wmX3\",\"wmX4\":\"wmX4\",\"wmX5\":\"wmX5\",\"wmX6\":\"wmX6\",\"wmX7\":\"wmX7\",\"wmX8\":\"wmX8\"}]}]}";

    public static void show(Context context, String className, String type) {
        Intent intent = new Intent(context, ShangjiaAcitivity.class);
        intent.putExtra("className", className);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_shangjia;
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        className = getIntent().getStringExtra("className");
        type = getIntent().getStringExtra("type");
        setTitle(className);
        initTitle();
        initAdapter();

    }

    @Override
    protected void onRightButtonClick() {
        super.onRightButtonClick();
    }

    private void initTitle() {

        //监听交接单号 快递单号.

        etJiaojiedan.setOnKeyListener(new View.OnKeyListener() {

            @Override
            public boolean onKey(View v, int i, KeyEvent keyEvent) {
                Logutil.print("keycode " + keyEvent.getKeyCode());
                z_toast t_dm = new z_toast();
//                if (i == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_DOWN )
                    if (i == KeyEvent.KEYCODE_ENTER
                            && keyEvent.getAction() == KeyEvent.ACTION_UP)

                {
                    //  t_dm.showToast("保存中。。。",ShangjiaAcitivity.this);
                    if (TextUtils.isEmpty(etJiaojiedan.getText().toString())) {
                        t_dm.showToast("交接单号不能为空！",ShangjiaAcitivity.this);
                        return false;
                    }
                    // getData();
                    reFresh();
                    final int LONG_DELAY = 1000;
                   // final EditText et_fangweima = (EditText) findViewById(R.id.et_fangweima);

                   // etJiaojiedan.setText("");
                    //执行清空动作 2018/09/29
                    return true;
                }


                return false;
            }
        });



        //监听回车键 交接单号


        headHolder.setVisibility(View.GONE);
        if (className.equals(ClassHolder.SHANGJIA)) {
            UpdataUiUtil.getDefaul().setText("物料号", tvWuliaohao).setText("交接数量", tvJiaojieshu)
                    .setText("待上架数量", tvDaishangjia).setText("", tvHeji)
                    .setText("交接单:", tvJiaojiedan).setText("防伪码:", tvFangweima);
        } else if (className.equals(ClassHolder.CHENGPINXIAJIA)) {
            UpdataUiUtil.getDefaul().setText("物料号", tvWuliaohao).setText("发货数", tvJiaojieshu)
                    .setText("建议储位", tvDaishangjia).setText("待下架数量", tvHeji)
                    .setText("单号：", tvJiaojiedan).setText("防伪码：", tvFangweima).setText("储位：", tvChuwei);
            llChuwei.setVisibility(View.VISIBLE);
            divChuwei.setVisibility(View.VISIBLE);
         // 成品下架增加扫描盒马和条码的接口，扫完之后传输给SMS ，SMS 进行审核过账 2019/02/20.  需要增加回车键监听.2019/02/20 create it_yeys

            //回车监听
            etFangweima.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
            etFangweima.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View view, int i, KeyEvent keyEvent) {
                    z_toast t_dm = new z_toast();
                    if (i == KeyEvent.KEYCODE_ENTER  ) {
                        final int LONG_DELAY = 1000;
                        final EditText et_fangweima = (EditText) findViewById(R.id.et_fangweima);
                        Logutil.print("输入的盒马:"+et_fangweima.getText().toString());
                        if(!StringUtil.isEmpty(et_fangweima.getText().toString())){
                            reFreshds();
                            et_fangweima.setText("");
                        }
                        //执行清空动作 2018/09/29
                        return true;
                    }
                    return false;
                }
            });



            // 成品下架增加扫描盒马和条码的接口，扫完之后传输给SMS ，SMS 进行审核过账 2019/02/20.

        }

        else if (className.equals(ClassHolder.YLSJ)) {
            UpdataUiUtil.getDefaul().setText("物料号", tvWuliaohao).setText("发货数", tvJiaojieshu)
                    .setText("建议储位", tvDaishangjia).setText("待下架数量", tvHeji)
                    .setText("单号：", tvJiaojiedan);

        }

        else if (className.equals(ClassHolder.FAHUOXIAJIA)) {
            UpdataUiUtil.getDefaul().setText("物料号", tvWuliaohao).setText("建议储位", tvJiaojieshu)
                    .setText("发货数量", tvDaishangjia).setText("未扫描数量", tvHeji)
                    .setText("快递单：", tvJiaojiedan).setText("防伪码：", tvFangweima).setText("储位：", tvChuwei);
            llChuwei.setVisibility(View.VISIBLE);
            divChuwei.setVisibility(View.VISIBLE);
            //监听储位放生变化，校验下储位是否存在.


            //扫描回车键监听

            etFangweima.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
            etFangweima.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View view, int i, KeyEvent keyEvent) {
                    Logutil.print("keycode " + keyEvent.getKeyCode());
                    z_toast t_dm = new z_toast();
                    //    LoadingUtil.hideLoading();

                    if (i == KeyEvent.KEYCODE_ENTER  ) {
                      //  t_dm.showToast("保存中。。。",ShangjiaAcitivity.this);
                         final int LONG_DELAY = 1000;
                      //  ToastUtil.show(getActivity(), "已保存---", Toast.LENGTH_SHORT);
                     //   Toast.makeText(ShangjiaAcitivity.this,"已保存---",Toast.LENGTH_SHORT).show();
                            reFreshds();
                        final EditText et_fangweima1 = (EditText) findViewById(R.id.et_fangweima);
                        et_fangweima1.setText("");
                        //执行清空动作 2018/09/29
                            return true;
                        }
                    return false;
                }
            });
        } else if (className.equals(ClassHolder.CAIGOUTUIHUO)) {
            UpdataUiUtil.getDefaul().setText("物料号", tvWuliaohao).setText("", tvJiaojieshu)
                    .setText("入库数", tvDaishangjia).setText("待上架", tvHeji)
                    .setText("物料凭证：", tvJiaojiedan).setText("库位：", tvFangweima);
        } else if (className.equals(ClassHolder.XIAJIA)) {
            UpdataUiUtil.getDefaul().setText("物料号", tvWuliaohao).setText("发料数量", tvJiaojieshu)
                    .setText("待下架数量", tvDaishangjia).setText("", tvHeji)
                    .setText("物料凭证：", tvJiaojiedan);
            llSearch.setVisibility(View.GONE);
            divSearch.setVisibility(View.GONE);
        } else if (className.equals(ClassHolder.PANDIAN)) {
            UpdataUiUtil.getDefaul()
                    .setText("盘点单：", tvJiaojiedan).setText("物料/条码：", tvFangweima).setText("储位：", tvChuwei);
           llChuwei.setVisibility(View.VISIBLE);
            divChuwei.setVisibility(View.VISIBLE);
            llWuliaohao.setVisibility(View.GONE);
            divSearch.setVisibility(View.GONE);
        } else if (className.equals(ClassHolder.JIT)) {
//            headHolder.setVisibility(View.VISIBLE);
//            head.setVisibility(View.GONE);
            UpdataUiUtil.getDefaul().setText("物料号", tvWuliaohao).setText("建议储位", tvJiaojieshu)
                    .setText("发货数量", tvDaishangjia).setText("未扫描数量", tvHeji)
                    .setText("装箱清单：", tvJiaojiedan).setText("防伪码：", tvFangweima).setText("储位：", tvChuwei).setText("箱号:",tvxianghao);
            llChuwei.setVisibility(View.VISIBLE);
            divChuwei.setVisibility(View.VISIBLE);
            llxianghao.setVisibility(View.VISIBLE);
            divxianghao.setVisibility(View.VISIBLE);
   //回车监听
            etFangweima.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
            etFangweima.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View view, int i, KeyEvent keyEvent) {
                   // Logutil.print("测试次数keycode " + keyEvent.getKeyCode());
                    z_toast t_dm = new z_toast();
                    //    LoadingUtil.hideLoading();

                    if (i == KeyEvent.KEYCODE_ENTER  ) {
                       // Logutil.print("测试兼职keycode " + keyEvent.getKeyCode());
                        //  t_dm.showToast("保存中。。。",ShangjiaAcitivity.this);
                        final int LONG_DELAY = 1000;
                        //  ToastUtil.show(getActivity(), "已保存---", Toast.LENGTH_SHORT);
                        //   Toast.makeText(ShangjiaAcitivity.this,"已保存---",Toast.LENGTH_SHORT).show();
                        final EditText et_fangweima1 = (EditText) findViewById(R.id.et_fangweima);

                        if(!StringUtil.isEmpty(et_fangweima1.getText().toString())){
                            reFreshds();
                            et_fangweima1.setText("");
                        }

                        //执行清空动作 2018/09/29
                        return true;
                    }
                    return false;
                }
            });

        } else if (className.equals(ClassHolder.YIKU2)) {
            UpdataUiUtil.getDefaul().setText("物料号", tvWuliaohao).setText("", tvJiaojieshu)
                    .setText("数量", tvDaishangjia).setText("待移库", tvHeji)
                    .setText("预留单：", tvJiaojiedan).setText("唯一码：", tvFangweima);
        }else if (className.equals(ClassHolder.CHENGPINTUIHUO)) {
            UpdataUiUtil.getDefaul().setText("物料号", tvWuliaohao).setText("退货数", tvJiaojieshu)
                    .setText("建议储位", tvDaishangjia).setText("待上架数量", tvHeji)
                    .setText("退货小票：", tvJiaojiedan).setText("防伪码：", tvFangweima);
        }
    }


    private void initAdapter() {
        // || className.equals(ClassHolder.JIT)
        if (className.equals(ClassHolder.PANDIAN)) {
            adapter = new DemoAdapter(getActivity());
            adapter.setClassName(className);
            adapter.setTypeName(type);
            SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(new SwipeDismissAdapter(adapter, this));
            swingBottomInAnimationAdapter.setInitialDelayMillis(300);
            swingBottomInAnimationAdapter.setAbsListView(lvMylistview);
            lvMylistview.setDivider(new ColorDrawable(getResources().getColor(R.color.app_bg)));
            lvMylistview.setDividerHeight(10);
            lvMylistview.setAdapter(swingBottomInAnimationAdapter);
            View head = LayoutInflater.from(this).inflate(R.layout.headimg_listview, null);
            lvMylistview.addHeaderView(head);
        } else {
            adapter = new DemoAdapter(getActivity());
            adapter.setClassName(className);
            lvMylistview.setAdapter(adapter);
            lvMylistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    if (!className.equals(ClassHolder.PANDIAN) && !className.equals(ClassHolder.KUCUNCHAXUN) && !className.equals(ClassHolder.YICHU))
                        ShangjiaDetailActivity.show(ShangjiaAcitivity.this, type, ClassHolder.change2Detail(className), list.get(i), i);
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEventBus(MessageEvent msg) {
        //刷新界面
        if (msg.getKey().equals("key1")) {
            int position = msg.getMessage4() + 1;
            if (list.size() > position)
                EventBus.getDefault().post(new MessageEvent("key2", list.get(position), position));
            else
                EventBus.getDefault().post(new MessageEvent("key3"));
            reFresh();
        } else if (msg.getKey().equals("key4")) {
            int position = msg.getMessage4() + 1;
            if (list.size() > position)
                EventBus.getDefault().post(new MessageEvent("key2", list.get(position), position));
            else
                EventBus.getDefault().post(new MessageEvent("key5"));
                   reFresh(false);
        } else if (msg.getKey().equals("key6")) {
            reFresh();
        }
    }
    //获取列表
   public void getData(final boolean isShaoMiao, final boolean isShowLoading) {
    if (!ClickFastUtil.isFastClick1000())
////            return;
//         String dl = null;
//            //更改  如果是成品发货的先执行传输SMS 传输回来之后在判断是否执行到JEECG后台的保存. create by it_yeys 2019/02/20
//            if ( isShaoMiao && className.equals("成品拣货"))
//            {
//          //    if (className.equals("成品拣货")){
//                Logutil.print("调用SMS盒马接口！");
//                String url1 = null;
//                url1 = "http://192.168.254.63:8080/billmanage/ctrl/ship/v1/deliveryByLno?lno=8000030661&barcode=0121736817624710&operator=DC01";
//                HTTPUtils.get(getActivity(),url1, new VolleyListener() {
//                    @Override
//                    public void onResponse(String response) {
//                        //  传输给SMS 的发货返回的信息 create by it_ysye 2019/02/20
//                        Logutil.print("response==" + response);
//                        Zhmsm cpxj_smshma = GsonUtils.parseJSON(response, Zhmsm.class);
//                        Logutil.print(cpxj_smshma.getCode().toString());
//                        if (cpxj_smshma.getCode().toString().equals("0")) {
//                            Logutil.print("发货成功");
//                        final  String dl = "X";
//                        } else {
//                            Logutil.print("发货失败" + cpxj_smshma.getMsg());
//                        }
//
//                      //    传输给SMS 的发货返回的信息
//                    }
//
//                    @Override
//                    public void onErrorResponse(VolleyError error) {
//
//                    }
//                });
//
//         }
//                        //  传输给SMS 的发货返回的信息
//






                //更改  如果是成品发货的先执行传输SMS 传输回来之后在判断是否执行到JEECG后台的保存. create by it_yeys 2019/02/20
                Logutil.print("运行getData");
            //判断如果dl = X 且 执行的拣货下架.


                String url = getUrl(isShaoMiao);
                Logutil.print("shangjia--url--" + url);


//        //监听判断如果仓位不存在，提示错误不给保存
//        String cwurl = "http://192.168.200.100/zzwms/rest/wmbaseapi/getlgpla/"+etChuwei.getText().toString();
//        Logutil.print("监听仓位是否存在"+cwurl);
//        HTTPUtils.get(getActivity(), cwurl, new VolleyListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                z_toast ts = new z_toast();
//                ts.showToast("接口调用失败！",ShangjiaAcitivity.this);
//            }
//
//            @Override
//            public void onResponse(String response) {
//                ResultDO vm = GsonUtils.parseJSON(response, ResultDO.class);
//                if(!vm.isOK()){
//                    z_toast ts = new z_toast();
//                    ts.showToast("仓位不存在!",ShangjiaAcitivity.this);
//
//                }
//            }
//        });
//
//
//        //监听判断如果仓位不存在，提示错误不给保存


                final long start = System.currentTimeMillis();
                Logutil.print("开始时间---" + start);
                HTTPUtils.get(getActivity(), url, new VolleyListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Logutil.print("花费时间---" + (start - System.currentTimeMillis()));
//                if(isShowLoading)
//                LoadingUtil.hideLoading();
                    }

                    @Override
                    public void onResponse(String response) {
                        //  传输给SMS 的发货返回的信息 create by it_ysye 2019/02/20
//                        Logutil.print("response==" + response);
//                        Zhmsm cpxj_smshma = GsonUtils.parseJSON(response, Zhmsm.class);
//
//                        if (cpxj_smshma.getCode() == "0") {
//                            Logutil.print("发货成功");
//                        } else {
//                            Logutil.print("发货失败" + cpxj_smshma.getMsg());
//                        }

                        //  传输给SMS 的发货返回的信息


                        if (isShowLoading)
//                LoadingUtil.hideLoading();
                            if (list != null)
                                list.clear();
                        Logutil.print("花费时间---" + (start - System.currentTimeMillis()));
                        //   Logutil.print("response==" + response);
                        DemoListVm vm = GsonUtils.parseJSON(response, DemoListVm.class);
                        if (!vm.isOk()) {
                            // 清空最后一次返回清空数据，重新调用.
                            if (vm.getObj() != null) {
                                list = vm.getObj();
                            } else {
                                list.clear();

                            }
                            // 清空最后一次返回清空数据，重新调用.
                            z_toast ts = new z_toast();
                            ts.showToast(vm.getErrorMsg(), ShangjiaAcitivity.this);
                            Logutil.print("发货已经存在!" + vm.getErrorMsg());
                            Logutil.print("发货已经存在!" + vm.isOk());
                            //判断如果订单已经出库，清空单号！
                            if ("该订单已出库".equals(vm.getErrorMsg())) {
                                etJiaojiedan.setText("");
                                etJiaojiedan.requestFocus();
                                etChuwei.setText("");    // 订单已经出库的时候，清空交接单号和储位号/2018/11/1
                                // 设置震动和声音   2018/11/1 电商发货完成提示报警声音.

                                //       AudioManager am = (AudioManager) ShangjiaAcitivity.getSystemService(Service.AUDIO_SERVICE);
                                //   AudioManager audioManager =  (AudioManager)getSystemService(ShangjiaAcitivity.AUDIO_SERVICE);
                                //    audioManager.adjustStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_RAISE, AudioManager.FLAG_VIBRATE);
                                Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                                //   vibrator.vibrate(1);
                                vibrator.vibrate(new long[]{300, 500}, -1);
                                Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                                Ringtone rt = RingtoneManager.getRingtone(getApplicationContext(), uri);
                                rt.play();
                                //  vibrator.vibrate();
                                // 已经成功执行铃声
                                Logutil.print("已经成功执行响铃动作!");
                                //    audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
//                        MediaPlayer mp = new MediaPlayer();
//                       //  String mContext = ShangjiaAcitivity.ALARM_SERVICE;
//                        try {
//                            mp.setDataSource(mContext, RingtoneManager
//                                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                        try {
//                            mp.prepare();
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                        mp.start();
//                        mp.setLooping(true); //循环播放
                            }
                            //判断如果订单已经出库，清空单号！


                            // return;
                        } else {
                            if (vm != null) {
                                if (vm.getObj() != null) {
                                    list = vm.getObj();
                                }
                            }
                        }
                        adapter.setList(list);
                        adapter.notifyDataSetChanged();
                    }
                });
            }

    @Override
    protected void onResume() {
        Logutil.print("执行resume！");
        super.onResume();
        reFresh();
    }

    //获取拼接后的url
    public String getUrl(boolean isShaoMiao) {
        String url = Constance.gettSapLsqdControllerURL() + type;
        Logutil.print("打印点击按钮的值" + className + type);
        //判断是否移库 /JIT
        if (className.equals(ClassHolder.JIT)) {

            url += "?docid=" + etJiaojiedan.getText().toString().trim();
            if (StringUtil.isEmpty(etChuwei.getText().toString().trim())) {
                url += "&searchstr2=" + etFangweima.getText().toString().trim();
                if (isShaoMiao && type.equals("JIT")) {
                    url += "&searchstr3=enter" + "&searchstr4=" + etxianghao.getText().toString().trim();
                }
            } else {
                url += "&searchstr1=" + etChuwei.getText().toString().trim() + "&searchstr2=" + etFangweima.getText().toString().trim();
                if (isShaoMiao && type.equals("JIT")) {
                    url += "&searchstr3=enter" + "&searchstr4=" + etxianghao.getText().toString().trim();
                    ;
                }
            }


//            url += "?docid=" + etWerks2.getText().toString().trim()  + "&searchstr1=" +
//                    etLgort2.getText().toString() + "&searchstr2=" + etChuwei2.getText().toString().trim()
//                    + "&searchstr3=" + etFangweima2.getText().toString().trim() ;
             //成品拣货 扫描条码
        } else if( className.equals(ClassHolder.CHENGPINXIAJIA)){

            url += "?docid=" + etJiaojiedan.getText().toString().trim();
            if (StringUtil.isEmpty(etChuwei.getText().toString().trim())) {
                url += "&searchstr2=" + etFangweima.getText().toString().trim();
                if (isShaoMiao && type.equals("JHXJ")) {
                    url += "&searchstr3=enter" + "&searchstr4=" ;
                }
            } else {
                url += "&searchstr1=" + etChuwei.getText().toString().trim() + "&searchstr2=" + etFangweima.getText().toString().trim();
                if (isShaoMiao && type.equals("JHXJ")) {
                    url += "&searchstr3=enter" + "&searchstr4=";
                    ;
                }
            }













      }else if (className.equals(ClassHolder.YLSJ)) {
            //增加预留上架
            url = "http://192.168.200.100/zzwms/rest/wmapi/getdoc/YLYC?docid=" + etJiaojiedan.getText().toString() + "&searchstr2=" + etFangweima.getText().toString();
        }
//        }else if(className.equals(ClassHolder.CHENGPINXIAJIA)) {
//            //参数盒马/条码+单号    create by it_yeys 2019/02/20
//
//            url += "?docid=" + etJiaojiedan.getText().toString().trim() ;
//            if (StringUtil.isEmpty(etChuwei.getText().toString().trim() )) {
//                url += "&searchstr2=" + etFangweima.getText().toString().trim() ;
//                if(isShaoMiao&&type.equals("CHENGPINXIAJIA")){
//                    url +=   "&searchstr3=enter" + "&searchstr4=" + etxianghao.getText().toString().trim();
//                }
//            } else {
//                url += "&searchstr1=" + etChuwei.getText().toString().trim()  + "&searchstr2=" + etFangweima.getText().toString().trim() ;
//                if(isShaoMiao&&type.equals("CHENGPINXIAJIA")){
//                    url +=   "&searchstr3=enter" + "&searchstr4=" + etxianghao.getText().toString().trim();;
//                }
//            }
//
//            url = "http://192.168.254.63:8080/billmanage/ctrl/ship/v1/deliveryByLno?lno=";
//            Logutil.print("已经触发新的SMS 盒马接口!");

     //   }
        else {
            url += "?docid=" + etJiaojiedan.getText().toString().trim() ;
            if (StringUtil.isEmpty(etChuwei.getText().toString().trim() )) {
                url += "&searchstr2=" + etFangweima.getText().toString().trim() ;
                if(isShaoMiao&&type.equals("DSXJ")){
                    url +=   "&searchstr3=enter";
                }
            } else {
                url += "&searchstr1=" + etChuwei.getText().toString().trim()  + "&searchstr2=" + etFangweima.getText().toString().trim() ;
                if(isShaoMiao&&type.equals("DSXJ")){
                    url +=   "&searchstr3=enter";
                }
            }
        }
        url +=  "&username="+ SharedPreferencesUtil.getInstance(this).getKeyValue(Constance.SHAREP.LOGINNAME);
        Logutil.print("最终url---"+url);
        return url;
    }

    public void reFreshds() {
        getData(true,true);
    }
    public void reFresh() {
        getData(false,true);
    }
    public void reFresh(boolean isShowLoading) {
        getData(false,isShowLoading);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CaptureActivity.SCANNIN_GREQUEST_CODE && resultCode == Activity.RESULT_OK) {
            Bundle bundle = data.getExtras();
            String content = bundle.getString("result");
            z_toast  tk = new z_toast();
                 tk.showToast(content,ShangjiaAcitivity.this);
         }
        reFresh();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }




    @OnClick({R.id.tv_search, R.id.tv_search2,R.id.ll_scan})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.ll_scan:
                if (PermissionUtils.checkCameraPermissions(this))
                    CaptureActivity.show(this);
                break;
            case R.id.tv_search:
                z_toast  tk = new z_toast();
              //  tk.showToast(result.getErrorMsg(),Resum_Activity.this);
                if (StringUtil.isEmpty(etJiaojiedan.getText().toString())) {
                   // ToastUtil.show(this, "请输入单号！");
                    tk.showToast("请输入单号！",ShangjiaAcitivity.this);
                    return;
                }
                if (!StringUtil.isEmpty(etJiaojiedan.getText().toString())) {
                  //  ToastUtil.show(this, "搜索中。。");
                    tk.showToast("搜索中。。。",ShangjiaAcitivity.this);
                  //  return;
                }

                reFresh();
                break;
            case R.id.tv_search2:
                z_toast  tk1 = new z_toast();
                if (StringUtil.isEmpty(etWerks2.getText().toString())) {
                   // ToastUtil.show(this, "请输入工厂！");
                    tk1.showToast("请输入工厂。。。",ShangjiaAcitivity.this);
                    return;
                }
                reFresh();
                break;
        }
    }


    @Override
    public void onDismiss(@NonNull ViewGroup listView, @NonNull int[] reverseSortedPositions) {

    }
}
